package com.linlang.grasharepro.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.activity.SearchResultActivity;
import com.linlang.grasharepro.dao.UserDB;
import com.linlang.grasharepro.model.User;
import com.linlang.grasharepro.view.MyGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lixin on 2016-8-6.
 */
public class SearchPage extends Fragment {

    private View view;
    private ImageView tuan_button1;
    private EditText et_searchContent;
    private UserDB userDB;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.searchpage, container, false);
        initView();
        return view;
    }

    private void initView() {
        tuan_button1 = (ImageView) view.findViewById(R.id.Tuan_button1);
        tuan_button1.setOnClickListener(new MyOnClickListener());

        et_searchContent = (EditText) view.findViewById(R.id.et_searchContent);

        MyGridView grid_school = (MyGridView) view.findViewById(R.id.grid_school);
        MyGridView grid_profe = (MyGridView) view.findViewById(R.id.grid_profe);
        MyGridView grid_gra = (MyGridView) view.findViewById(R.id.grid_gra);
        ArrayList<HashMap<String, Object>> schoolgrid = this.creatSchoolGrid();
        ArrayList<HashMap<String, Object>> profegrid = this.creatProfeGrid();
        ArrayList<HashMap<String, Object>> gragrid = this.creatGraGrid();

        SimpleAdapter schoolgridItems = new SimpleAdapter(getContext(), schoolgrid,
                R.layout.item_grid, new String[]{"ItemImage", "ItemText"},
                new int[]{R.id.item_grid_image, R.id.item_grid_text});
        SimpleAdapter profegridItems = new SimpleAdapter(getContext(), profegrid,
                R.layout.item_grid, new String[]{"ItemImage", "ItemText"},
                new int[]{R.id.item_grid_image, R.id.item_grid_text});
        SimpleAdapter gragridItems = new SimpleAdapter(getContext(), gragrid,
                R.layout.item_grid, new String[]{"ItemImage", "ItemText"},
                new int[]{R.id.item_grid_image, R.id.item_grid_text});
        grid_school.setAdapter(schoolgridItems);// 添加适配器
        grid_profe.setAdapter(profegridItems);// 添加适配器
        grid_gra.setAdapter(gragridItems);// 添加适配器

        grid_school.setOnItemClickListener(new ItemClickListener());
        grid_profe.setOnItemClickListener(new ItemClickListener());
        grid_gra.setOnItemClickListener(new ItemClickListener());
    }

    private ArrayList<HashMap<String, Object>> creatGraGrid() {
        ArrayList<HashMap<String, Object>> gragrid = new ArrayList<HashMap<String, Object>>();

        HashMap<String, Object> map1 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map2 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map3 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map4 = new HashMap<String, Object>();// 建立hashmap对象
        map1.put("ItemImage", R.drawable.jieshu0);
        map2.put("ItemImage", R.drawable.jieshu1);
        map3.put("ItemImage", R.drawable.jieshu2);
        map4.put("ItemImage", R.drawable.jieshu3);
        map1.put("ItemText", "2014届");
        map2.put("ItemText", "2015届");
        map3.put("ItemText", "2016届");
        map4.put("ItemText", "2017届");
        gragrid.add(map1);
        gragrid.add(map2);
        gragrid.add(map3);
        gragrid.add(map4);
        return gragrid;
    }

    private ArrayList<HashMap<String, Object>> creatProfeGrid() {
        ArrayList<HashMap<String, Object>> profegrid = new ArrayList<HashMap<String, Object>>();

        HashMap<String, Object> map1 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map2 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map3 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map4 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map5 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map6 = new HashMap<String, Object>();// 建立hashmap对象
        map1.put("ItemImage", R.drawable.zhuanye1);
        map2.put("ItemImage", R.drawable.zhuanye2);
        map3.put("ItemImage", R.drawable.zhuanye3);
        map4.put("ItemImage", R.drawable.zhuanye4);
        map5.put("ItemImage", R.drawable.zhuanye5);
        map6.put("ItemImage", R.drawable.zhuanye6);
        map1.put("ItemText", "法律");
        map2.put("ItemText", "中文");
        map3.put("ItemText", "会计");
        map4.put("ItemText", "计算机与技术");
        map5.put("ItemText", "农业科技");
        map6.put("ItemText", "设计艺术");
        profegrid.add(map1);
        profegrid.add(map2);
        profegrid.add(map3);
        profegrid.add(map4);
        profegrid.add(map5);
        profegrid.add(map6);
        return profegrid;
    }

    private ArrayList<HashMap<String, Object>> creatSchoolGrid() {
        ArrayList<HashMap<String, Object>> schoolgrid = new ArrayList<HashMap<String, Object>>();

        HashMap<String, Object> map1 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map2 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map3 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map4 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map5 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map6 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map7 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map8 = new HashMap<String, Object>();// 建立hashmap对象
        HashMap<String, Object> map9 = new HashMap<String, Object>();// 建立hashmap对象
        map1.put("ItemImage", R.drawable.daxue0);
        map2.put("ItemImage", R.drawable.daxue1);
        map3.put("ItemImage", R.drawable.daxue2);
        map4.put("ItemImage", R.drawable.daxue3);
        map5.put("ItemImage", R.drawable.daxue4);
        map6.put("ItemImage", R.drawable.daxue5);
        map7.put("ItemImage", R.drawable.daxue6);
        map8.put("ItemImage", R.drawable.daxue7);
        map9.put("ItemImage", R.drawable.daxue8);
        map1.put("ItemText", "沈阳建筑大学");
        map2.put("ItemText", "沈阳工业大学");
        map3.put("ItemText", "沈阳理工大学");
        map4.put("ItemText", "沈阳教育学院");
        map5.put("ItemText", "沈阳药科大学");
        map6.put("ItemText", "湖北工程学院");
        map7.put("ItemText", "华中师范大学");
        map8.put("ItemText", "长安大学");
        map9.put("ItemText", "沈阳化工大学");
        schoolgrid.add(map1);
        schoolgrid.add(map2);
        schoolgrid.add(map3);
        schoolgrid.add(map4);
        schoolgrid.add(map5);
        schoolgrid.add(map6);
        schoolgrid.add(map7);
        schoolgrid.add(map8);
        schoolgrid.add(map9);
        return schoolgrid;
    }

    /**
     * GridView 点击事件
     */
    class ItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            HashMap<String, Object> item = (HashMap<String, Object>) parent.getItemAtPosition(position);
            String ny = (String) item.get("ItemText");

            Bundle bundle = new Bundle();
            bundle.putSerializable("searchContent", ny);
            Intent intent = new Intent(getActivity(),
                    SearchResultActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }

    class MyOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            Bundle bundle = new Bundle();
            bundle.putSerializable("searchContent", et_searchContent.getText().toString());
            Intent intent = new Intent(getActivity(),
                    SearchResultActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }
}
